package com.sanjiahantang.youjiarole.module.me.order.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderGoods;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderListBean;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zy on 2017/9/26.
 */

public class OrderListAdapter extends RecyclerView.Adapter {

    private Context context;

    public List<OrderListBean.Data> getData() {
        return data;
    }

    public void setData(OrderListBean data) {
        if(this.data == null){
            this.data = data.getData();
        }else {
            this.data.clear();
            this.data.addAll(data.getData());
        }

        typeList.clear();
        for (OrderListBean.Data item : this.data){
            typeList.add(typeMatcher.get(item.getStatus()));
        }
    }

    private List<OrderListBean.Data> data;
    private LayoutInflater inflater;
    private ArrayList<Integer> typeList;
    private HashMap<String,Integer> typeMatcher;
    private HashMap<String,String> typeNameMatcher;
    public OrderListAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
        typeMatcher = new HashMap<>();
        typeMatcher.put("un", OrderView.TYPE_UN_PAY);
        typeMatcher.put("part", OrderView.TYPE_PART_PAY);
        typeMatcher.put("ed", OrderView.TYPE_PAYED);
        typeMatcher.put("com", OrderView.TYPE_COMPLETE);
        typeNameMatcher = new HashMap<>();
        typeNameMatcher.put("un","待付款");
        typeNameMatcher.put("part","部分付款");
        typeNameMatcher.put("ed","已付款");
        typeNameMatcher.put("com","已完成");

        typeList = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (typeList.get(viewType)){
            case OrderView.TYPE_UN_PAY:return new UnPayHolder(inflater.inflate(R.layout.item_me_order,null),data.get(viewType));
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    private class UnPayHolder extends RecyclerView.ViewHolder{

        public UnPayHolder(View itemView, OrderListBean.Data data) {
            super(itemView);
            TextView txtOrderNum = (TextView) itemView.findViewById(R.id.txtOrderNum);
            TextView txtState = (TextView) itemView.findViewById(R.id.txtState);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.txtGoodsCount);
            TextView txtAmount = (TextView) itemView.findViewById(R.id.txtAmount);
            TextView txtButton = (TextView) itemView.findViewById(R.id.txtButton);
            TextView txtButton2 = (TextView) itemView.findViewById(R.id.txtButton2);
            ListView goodListView = (ListView) itemView.findViewById(R.id.goodListView);

            txtOrderNum.setText(data.getOrder_sn());
            txtState.setText(typeNameMatcher.get(data.getStatus()));
            float price = 0f;
            int count = 0;
            List<OrderGoods> goods = data.getGoods();
            for (OrderGoods item : goods) {
                price += Float.parseFloat(item.getGoods_price());
                count += Integer.parseInt(item.getGoods_number());
            }
            txtGoodsCount.setText(String.valueOf(count));
            txtAmount.setText(String.valueOf(price));

            goodListView.setAdapter(new QuickAdapter<OrderGoods>(context,R.layout.item_order_goods,goods) {
                @Override
                protected void convert(BaseAdapterHelper helper, OrderGoods item) {
                    helper.setText(R.id.txtTitle,item.getGoods_name());
                    helper.setText(R.id.txtPrice,item.getGoods_price());
                    helper.setText(R.id.txtNumber,item.getGoods_number());
                    Picasso.with(context).load(FileUtils.rePath(item.getGoods_thumb())).into((ImageView) helper.getView(R.id.imgGoods));
                }
            });

            txtButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Utils.showToast(context,"付款");
                }
            });

            txtButton2.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Utils.showToast(context,"取消付款");
                }
            });
        }
    }
}
